package com.koala.dbUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OracleAgent {
	
	Connection conexion;
	ResultSet res=null;
	
	String url="jdbc:oracle:thin:@";
	
	String host;
	String puerto;
	String bd;
	
	String usuario;
	String pass;
	
	public OracleAgent(String host, String puerto) {
		this.url+=host+":"+puerto;
	}
	
	public void Conectar(String usuario, String pass, String bd){
		
		this.usuario=usuario;
		this.pass=pass;
		this.bd=bd;
		
		this.url+=":"+bd;
		
		try {
			
			DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
			
			conexion = DriverManager.getConnection(this.url, this.usuario, this.pass);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void EjecutarSQL(String sql){
		
		try {
			
			Statement stmt = conexion.createStatement();
			
			res = stmt.executeQuery(sql);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void CerrarConexion(){
		try {
		
			if(!conexion.isClosed())
				conexion.close();		
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ResultSet getRset() {
		return res;
	}

	protected void finalize() throws Throwable {
		try {
			
			if(!conexion.isClosed())
				conexion.close();		
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		super.finalize();
	}
	
}
